package com.authorityManagement.controller;

import com.authorityManagement.entity.User;
import com.authorityManagement.util.resultUtil.R;
import com.authorityManagement.service.UserService;
import com.authorityManagement.vo.TokenVo;
import com.authorityManagement.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
      
      @Autowired
      private UserService userService;
      
      @PostMapping("/login")
      public R login(@RequestBody User user) {
            Map<String,Object> datas = userService.login(user);
            return R.ok(200, "登录成功").data("loginInfo", datas);
      }
      
      @PostMapping("/logout")
      public R logout(HttpServletRequest request, HttpServletResponse response) {
            userService.logout(request, response);
            return R.ok(200,"用户退出成功");
      }
      
      @GetMapping("/refreshToken")
      public R refreshToken(HttpServletRequest request) {
            String token = request.getHeader("token");
            if (token == null) {
                  token = request.getParameter("token");
            }
            TokenVo tokenVo = userService.refreshToken(token);
            return R.ok().data("refreshToken", tokenVo);
      }
      
      
      @GetMapping("/queryUserInfo")
      public R queryUserInfo(){
            UserVo userVo = userService.queryUserInfo();
            return R.ok().data("userInfo",userVo);
      }
      
      @GetMapping("/queryAllUserList")
      public R queryAllUserList() {
            return R.ok().data("userList", userService.list());
      }
}
